package com.github.tvbox.osc.ui.activity;

import android.content.Context;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.github.tvbox.osc.ui.adapter.HomePageAdapter;
import com.github.tvbox.osc.ui.tv.widget.DefaultTransformer2;
import com.github.tvbox.osc.ui.tv.widget.FixedSpeedScroller;

import java.lang.reflect.Field;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;
import io.github.source.media.bean.MediaCategory;

/**
 * @ClassName: HomeViewPager
 * @Description: (HomeViewPager 处理首页面ViewPager需要展示的Fragment)
 * @author: huangrongcai
 * @date: 2024/6/28
 * @Copyright: 黄嵘才（HuangRc)
 */
public class ViewPagerManager {/*接口化*/
    private boolean initViewed = false;
    private ViewPager2 mViewPager;
    private HomePageAdapter mPageAdapter;
    private FragmentManager mFragmentManager;
    private final Lifecycle mLifecycle;

    public static ViewPagerManager create(@NonNull FragmentActivity fActivity, View mViewPager) {
        ViewPagerManager manager = new ViewPagerManager(fActivity);
        manager.setViewPager(mViewPager);
        return manager;
    }

    public static ViewPagerManager create(@NonNull Fragment fragment, View mViewPager) {
        ViewPagerManager manager = new ViewPagerManager(fragment);
        manager.setViewPager(mViewPager);
        return manager;
    }

    public ViewPagerManager(@NonNull FragmentActivity fragmentActivity) {
        this(fragmentActivity.getSupportFragmentManager(), fragmentActivity.getLifecycle());
    }

    public ViewPagerManager(@NonNull Fragment fragment) {
        this(fragment.getChildFragmentManager(), fragment.getLifecycle());
    }

    public ViewPagerManager(@NonNull FragmentManager fragmentManager,
                            @NonNull Lifecycle lifecycle) {
        mFragmentManager = fragmentManager;
        mLifecycle = lifecycle;
    }

    public void setCurrentItem(int position) {
        setCurrentItem(position, false);
    }

    public int getCurrentItem() {
        return mViewPager.getCurrentItem();
    }

    public void setCurrentItem(int position, boolean smoothScroll) {
        mViewPager.setCurrentItem(position, smoothScroll);
    }

    public ViewPager2 getViewPager() {
        return mViewPager;
    }

    /**
     * ？注意值传递和引用传递的关系
     *
     * @param list 新数据
     */
    public void setNewData(List<MediaCategory> list) {
        initViewPager();
        mPageAdapter.setData(list);
    }

    public void initViewPager() {
        if (!initViewed) {
            customizeViewPagerScroll();
            setupViewPager();
            initViewed = true;
        }
    }

    private void customizeViewPagerScroll() {
        try {
            Field field = ViewPager.class.getDeclaredField("mScroller");
            field.setAccessible(true);
            FixedSpeedScroller scroller = new FixedSpeedScroller(context(), new AccelerateInterpolator());
            field.set(mViewPager, scroller);
            // 假设setmDuration是一个正确的方法，这里更正方法名的首字母大小写
            scroller.setmDuration(300);
        } catch (Exception e) {
            // 增强的异常处理：记录异常信息
            e.printStackTrace();
        }
    }

    private void setupViewPager() {
        if (mPageAdapter == null) {
            mPageAdapter = new HomePageAdapter(mFragmentManager, mLifecycle);
        }
        mViewPager.setAdapter(mPageAdapter);
        mViewPager.setPageTransformer(new DefaultTransformer2());
        mViewPager.setCurrentItem(getCurrentSelected(), false); // 默认选中显示的
    }

    public void setOnPageChangeCallback(@NonNull ViewPager2.OnPageChangeCallback callback) {
        mViewPager.registerOnPageChangeCallback(callback);
    }

    public Context context() {
        return mViewPager.getContext();
    }

    private FragmentManager getFragmentManager() {
        if (mFragmentManager == null) {
            if (context() instanceof FragmentActivity) {
                mFragmentManager = ((FragmentActivity) context()).getSupportFragmentManager();
            }

        }
        return mFragmentManager;
    }

    public void setViewPager(View viewPager) {
        if (viewPager instanceof ViewPager2) {
            mViewPager = (ViewPager2) viewPager;
        } else {
            mViewPager = null;
        }
    }

    public int getCurrentSelected() {
        return 0;
    }

    public Fragment getFragment(int position) {
        Fragment fragment = mPageAdapter.getFragment(position);
        return fragment;
    }

}
